const { UUIDV4, UUID, STRING, TINYINT, DATE, CHAR, Model } = require('sequelize');
const {
    checkTheIdCard,
    checkThePhone,
    checkTheUsername,
    checkThePassword
} = require('../../helpers/validator');
const generateSalt = require('../../helpers/generate-salt');
const { MD5, enc } = require('crypto-js');
const db = require('../../helpers/db');

class UserSafe extends Model {}

UserSafe.init(
    {
        id: {
            type: UUID,
            defaultValue: UUIDV4,
            primaryKey: true,
            comment: '用户安全信息ID'
        },
        user: {
            type: UUID,
            unique: true,
            comment: '外键，用户ID'
        },
        username: {
            type: STRING(32),
            comment: '用户名',
            unique: true,
            validate: {
                customValidator(value) {
                    if (!checkTheUsername(value)) {
                        const error = new Error('用户名由6-32个字母、数字或下划线组成');
                        error.status = 400;
                        throw error;
                    }
                }
            }
        },
        password: {
            type: CHAR(32),
            comment: '用户密码',
            validate: {
                customValidator(value) {
                    if (!checkThePassword(value)) {
                        const error = new Error('密码由6-32个小写字母、大写字母和数字组成');
                        error.status = 400;
                        throw error;
                    }
                }
            }
        },
        IDCard: {
            type: STRING(18),
            field: 'id_card',
            comment: '用户身份证号码',
            unique: true,
            validate: {
                customValidator(value) {
                    if (value && !checkTheIdCard(value)) {
                        const error = new Error('请输入正确的身份证号码');
                        error.status = 400;
                        throw error;
                    }
                }
            }
        },
        realName: {
            type: STRING(16),
            field: 'real_name',
            comment: '用户真实姓名'
        },
        phone: {
            type: STRING(11),
            unique: true,
            comment: '用户联系电话',
            validate: {
                customValidator(value) {
                    if (value && !checkThePhone(value)) {
                        throw new Error('请输入正确的手机号码');
                    }
                }
            }
        },
        email: {
            type: STRING(320),
            unique: true,
            comment: '用户邮箱',
            validate: {
                isEmail: {
                    msg: '请输入正确的邮箱'
                }
            }
        },
        salt: {
            type: CHAR(6),
            comment: '用户密码盐值'
        },
        status: {
            type: TINYINT,
            defaultValue: 0,
            comment: '用户状态，0未激活，1已激活，2冻结账户，3注销账户'
        }
    },
    {
        tableName: 'user_safe',
        underscored: true,
        sequelize: db
    }
);

UserSafe.addHook('afterValidate', (user, options) => {
    const { password = '' } = user;
    if (password) {
        const salt = generateSalt();
        user.password = MD5(password, salt).toString(enc.Hex);
        user.salt = salt;
    }
});

module.exports = UserSafe;
